/// Copyright 2007-8 Faraday Media
/// 
/// Licensed under the Apache License, Version 2.0 (the "License"); 
/// you may not use this file except in compliance with the License. 
/// You may obtain a copy of the License at 
/// 
///   http://www.apache.org/licenses/LICENSE-2.0 
/// 
/// Unless required by applicable law or agreed to in writing, software 
/// distributed under the License is distributed on an "AS IS" BASIS, 
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
/// See the License for the specific language governing permissions and 
/// limitations under the License.
using System.CodeDom;
using System.Reflection;

namespace MetaSharp.XML.Strategies {
  /// <summary>
  /// Strategy for handling properties that already exist in the base class.
  /// </summary>
  public class BaseClassHandledStrategy : IPropertyStrategy {
    #region IStrategy Members
    /// <summary>
    /// Defines that this strategy is base code.
    /// </summary>
    public StrategyPriority Priority {
      get { return StrategyPriority.BaseCode; }
    }
    #endregion

    #region IPropertyStrategy Members
    /// <summary>
    /// Checks whether this strategy applies to a property by checking if the auto wrapper base class
    /// has this property.
    /// </summary>
    /// <param name="pProp">the property to check</param>
    /// <returns>true - strategies apply</returns>
    public bool AppliesToProperty(PropertyInfo pProp) {
      return typeof (AutoWrapperBase).GetProperty(pProp.Name) != null;
    }

    /// <summary>
    /// Applies this strategy to the given generated property by adding a base return statement.
    /// </summary>
    /// <param name="pContext">the context of the generation</param>
    /// <param name="pProp">the declared property</param>
    /// <param name="pGeneratedProp">the property being generated</param>
    /// <param name="pClass">the class being declared</param>
    public void Apply(GenerationContext pContext, PropertyInfo pProp, CodeMemberProperty pGeneratedProp,
                      CodeTypeDeclaration pClass) {
      pGeneratedProp.Attributes |= MemberAttributes.Override;

      pGeneratedProp.GetStatements.Add(
        new CodeMethodReturnStatement(new CodePropertyReferenceExpression(new CodeBaseReferenceExpression(), pProp.Name)));
    }

    #endregion


  }
}
